﻿using System.Globalization;
using System.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication.RSS
{
    internal class RSSFeedReader
    {

        public static RSSFeed Parse(XmlDocument Doc)
        {
            RSSFeed feed = new RSSFeed();
            XmlNodeList channel = Doc.GetElementsByTagName("channel");

            if (channel.Count == 0) { return feed; }

            foreach (XmlNode node in channel[0].ChildNodes)
            {
                switch (node.Name)
                {
                    case "title": feed.Title = node.InnerText; break;
                    case "description": feed.Description = node.InnerText; break;
                    case "link": feed.Link = FeedHelper.ConvertToUri(node.InnerText); break;
                    case "copyright": feed.Copyright = node.InnerText; break;
                    case "language": if (node.InnerText != string.Empty) { feed.Language = CultureInfo.GetCultureInfo(node.InnerText); } break;
                    case "managingEditor": feed.ManagingEditor = node.InnerText; break;
                    case "webMaster": feed.WebMaster = node.InnerText; break;
                    case "lastBuildDate": feed.LastBuildDate = FeedHelper.ConvertToDate(node.InnerText); break;
                    case "pubDate": feed.PublishDate = FeedHelper.ConvertToDate(node.InnerText); break;
                    case "generator": feed.Generator = node.InnerText; break;
                    case "image": FeedImage img = new FeedImage();
                        foreach (XmlNode imageNode in node.ChildNodes)
                        {
                            switch (imageNode.Name)
                            {
                                case "url": img.ImageUrl = FeedHelper.ConvertToUri(imageNode.InnerText); break;
                                case "title": img.Title = imageNode.InnerText; break;
                                case "link": img.Link = FeedHelper.ConvertToUri(imageNode.InnerText); break;
                                case "description": img.Description = imageNode.InnerText; break;
                                case "width": img.Width = FeedHelper.ConvertToInt(imageNode.InnerText); break;
                                case "height": img.Height = FeedHelper.ConvertToInt(imageNode.InnerText); break;
                            }
                        }
                        feed.Image = img;
                        break;
                    case "item":
                        RSSItem item = new RSSItem();
                        foreach (XmlNode itemNode in node.ChildNodes)
                        {
                            switch (itemNode.Name)
                            {
                                case "title": item.Title = itemNode.InnerText; break;
                                case "description": item.Description = itemNode.InnerText; break;
                                case "comments": item.Comments = FeedHelper.ConvertToUri(itemNode.InnerText); break;
                                case "link": item.Link = FeedHelper.ConvertToUri(itemNode.InnerText); break;
                                case "pubDate": item.PublishDate = FeedHelper.ConvertToDate(itemNode.InnerText); break;
                            }
                        }
                        feed.Add(item);
                        break;
                }
            }

            return feed;
        }
    }
}
